import { getAccessToken, removeAccessToken, setAccessToken } from '@/utils/accessToken';
import { Storage } from '@/utils/index';
import { isBoolean, update } from 'lodash';

// const token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuYmYiOjE2OTA4NjI0NTMsImxvZ2luVHlwZSI6IkNQIiwiZXhwIjoxNjkxMjk0NDUzLCJpYXQiOjE2OTA4NjI0NTMsInVzZXJJZCI6MX0.9CKhBSbatEvcI0LdBuHpfio_Y0xN48144SLL9tkVVt8';

const state = {
  accessToken: getAccessToken(),
  // accessToken: token,
  userInfo: {
    id: '',
    userName: '', // 用户名
    avatar: '', // 头像
    phone: '', // 手机号
    currRole: {}, // 当前角色信息
    roleList: [], // 角色列表
    resourceList: [], // 权限列表
  },
  roleItemId: '', // 切换职务
  permList: [], // 权限列表
  sessionEmployeeOrderIsAsc: undefined, //会话存档员工排序
};

const getters = {
  accessToken: (state) => state.accessToken,
  userInfo: (state) => {
    if (!state.userInfo.id) {
      return Storage.getLoc('userInfo');
    }
    return state.userInfo;
  },
  roleItemId: (state) => {
    if (!state.roleItemId) return Storage.getLoc('roleItemId');
    return state.roleItemId;
  },
  permList: (state) => {
    if (!state.permList.length) return Storage.getLoc('permList');
    return state.permList;
  },
  sessionEmployeeOrderIsAsc: (state) => {
    if (!state.sessionEmployeeOrderIsAsc) return Storage.getLoc('sessionEmployeeOrderIsAsc');
    return state.sessionEmployeeOrderIsAsc;
  },
};

const mutations = {
  SET_ACCESS_TOEKN: (state, accessToken) => {
    state.accessToken = accessToken;
    setAccessToken(accessToken);
  },
  SET_USER_INFO: (state, userInfo) => {
    state.userInfo = userInfo;
    Storage.setLoc({
      key: 'userInfo',
      value: userInfo,
    });
  },
  SET_USER_ROLE_INFO: (state, roleInfo) => {
    state.userInfo = {
      ...state.userInfo,
      currRole: roleInfo,
    };
    let userInfo = state.userInfo;
    if (!userInfo.id) userInfo = Storage.getLoc('userInfo');
    userInfo.currRole = roleInfo;
    Storage.setLoc({
      key: 'userInfo',
      value: userInfo,
    });
  },
  SET_ROLE_ITEM_ID: (state, roleItemId) => {
    state.roleItemId = roleItemId;
    Storage.setLoc({
      key: 'roleItemId',
      value: roleItemId,
    });
  },
  SET_PERM_LIST: async (state, list) => {
    state.permList = list;
    Storage.setLoc({
      key: 'permList',
      value: list,
    });
    return true;
  },
  SET_SESSION_EMPLOYEE_ORDER: async (state, isAsc) => {
    state.sessionEmployeeOrderIsAsc = isAsc;
    Storage.setLoc({
      key: 'sessionEmployeeOrderIsAsc',
      value: isAsc,
    });
  },
};

const actions = {
  setAccessToken: ({ commit }, token) => {
    commit('SET_ACCESS_TOEKN', token);
  },
  setUserInfo: async ({ commit }, userInfo) => {
    commit('SET_USER_INFO', userInfo);

    const { currRole } = userInfo;
    //有dealer，有则是经销商角色，他的id构成 【dealerCode+roleId】
    //没有，则视为门店角色，他的id构成 【dealerStoreCode+roleId】

    // const __id = /dealer/.test(currRole.roleKey) ? currRole.dealerCode + '-' + currRole.roleId : currRole.dealerStoreCode + '-' + currRole.roleId;
    // console.log('roleId', __id)
    // commit('SET_ROLE_ITEM_ID', __id);
    // await commit('SET_PERM_LIST', userInfo.resourceList || []);
    return true;
  },
  setUserRoleInfo: ({ commit }, roleInfo) => {
    commit('SET_USER_ROLE_INFO', roleInfo);
  },
  setUserPermList: ({ commit }, permList) => {
    commit('SET_PERM_LIST', permList);
  },
  setRoleItemId: ({ commit }, roleItemId) => {
    commit('SET_ROLE_ITEM_ID', roleItemId);
  },
  clearUserInfo: ({ commit }) => {
    commit('SET_ROLE_ITEM_ID', '');
    commit('SET_PERM_LIST', []);
    commit('SET_USER_INFO', {
      id: '',
      userName: '', // 用户名
      avatar: '', // 头像
      phone: '', // 手机号
      currRole: {}, // 当前角色信息
      roleList: [], // 角色列表
      resourceList: [], // 权限列表
    });
  },
  resetAccessToken: ({ commit }) => {
    commit('SET_ACCESS_TOEKN', '');
    removeAccessToken();
  },
  updateSessionEmployeeOrderIsAsc: ({ commit }, isAsc) => {
    commit('SET_SESSION_EMPLOYEE_ORDER', isAsc);
  },
  logout: async ({ dispatch, commit }) => {
    Storage.removeLoc('userInfo');
    Storage.removeLoc('storeId');
    Storage.removeLoc('permList');
    Storage.removeLoc('roleItemId');
    await commit('SET_SESSION_EMPLOYEE_ORDER', false);
    await dispatch('resetAccessToken');
    await dispatch('clearUserInfo');
    location.reload();
  },
};

export default { state, getters, mutations, actions };
